package cn.edu.nju.software.cdiy.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import cn.edu.nju.software.cdiy.util.BusinessConstant;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class UserInterceptor extends AbstractInterceptor {

	private String url;

	public void setUrl(String url) {
		//System.out.println("setUrl()"+url);
		this.url = url;
	}
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//System.out.println("=====intercept()"+invocation.getInvocationContext().getName());
		Map<String, Object> session = invocation.getInvocationContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		if (url == null) {
			String queryString = request.getQueryString();
			if (queryString == null) {
				queryString = "";
			} else {
				queryString = "?" + queryString;
			}
			//System.out.println("ServletPath:"+request.getServletPath()+queryString);
			session.put(BusinessConstant.S_USER_URL, request.getServletPath()+queryString);
		} else {
			//System.out.println("url:"+url);
			session.put(BusinessConstant.S_USER_URL, url);
		}
		
		return invocation.invoke();
	}

}
